// LEC				March 26, 2021
// 
//////////////////////////////////////////////////////////////////////////////////
// STATE SIZE PROJECT
// Generic Data Prep File (Part 1)
// for CShapes, Briding data, etc.
////////////////////////////////////////////////////////////////////////////////// 




////////////////////////////////////////////////////////////////////////////////// 
// Creates metropolitan-level file: metvars  (not used for core results)
// This file covers the sovereign states or the metropolitan cores of empires
//////////////////////////////////////////////////////////////////////////////////

cd $INDIR 
insheet using $STATEFILE, clear


// State is dependent territory
gen depend = 0
replace depend = 1 if holder!=id & holder!=.

bys id: egen depanytime = max(depend)

duplicates drop id year, force
xtset id year

// State just became independent
gen liberated = 0
replace liberated = 1 if depend==1 & f.depend==0

// Secession flag at the level of dependencies
bys holder year: egen depsec = sum(liberated)
gen depsec1 = 0
replace depsec1 = 1 if depsec>0 & depsec!=.

// Compute stats including dependencies
// empire: state has dependencies
// ndep: number of dependencies
// sarea: entire area includin both core and dependencies
bys holder year: egen empire = max(depend)
bys holder year: egen ndep = sum(depend)
bys holder year: egen sarea = sum(area)

// From now on, get rid of the dependencies
drop if holder != id
drop holder  // use state id var as the main index

// calculate metropolis-specific vars
gen metarea = area
gen sharemet = area/sarea
drop area

sort id year



sort id year

cd $OUTDIR
save metvars, replace



////////////////////////////////////////////////////////////////////////////////// 
// Creates state-level file: statvars  (essential)
// This file holds state-level data of aggregate state units including dependencies
//////////////////////////////////////////////////////////////////////////////////

cd $INDIRAGG
insheet using $AGGSTATEFILE, clear

xtset id year
sort id year

// Merge in metvars (see above)
cd $OUTDIR
merge id year using metvars
drop _merge



// Continuity id coding
// idc is the extended id code that offers continuity based corrections to follow
gen idc = id

// Here are the explicit corrections that guarantee continuity of governance
if $IDCONT {
replace idc = 300 if id == 305 // Habsburgs --> Austria (no continuity for Hungary)
drop if idc == 300 & area == .
replace idc = 345 if id == 340 // Serbia & Montenegro --> Serbia
drop if idc == 345 & area == .
replace idc = 315 if id == 316 // Czechoslovakia --> Czech Rep.
drop if idc == 315 & area == .
replace idc = 560 if id == 563 // Transvaal --> South Africa
drop if idc == 560 & area == .
}

// Now compute state births and deaths
bys idc: egen maxyear = max(year) if area!=.
bys idc: egen minyear = min(year) if area!=.

sort idc year
xtset idc year

gen birth = 0
replace birth = 1 if area!=. & l.area==. & year>$STARTYEAR
gen firstbirth = 0
replace firstbirth = 1 if birth==1 & year==minyear
gen death = 0
replace death = 1 if l.area!=. & area==. & year<$ENDYEAR
gen finaldeath = 0
replace finaldeath = 1 if death == 1 & year == maxyear+1

egen name_id = group(name)
gen namechange = 0
replace namechange = 1 if l.name_id!=name_id & l.name_id!=. & year>$STARTYEAR

sort id year
cd $OUTDIR
save statevars, replace





